import { _decorator, Component, Node, Prefab, instantiate, CCInteger, CCFloat, Sprite, Color } from 'cc';
import { EllipseOrbitController } from './EllipseOrbitController';
import { Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('BallGenerator')
export class BallGenerator extends Component {
    @property(Node)
    ballPrefab: Node = null!; // 球体预制体

    @property(Node)
    orbitControllerNode: Node = null!; // 椭圆轨道控制器节点

    @property(CCInteger)
    initialBallCount: number = 1; // 初始球体数量

    @property(CCInteger)
    maxBallCount: number = 10; // 最大球体数量

    @property(CCFloat)
    autoAddInterval: number = 3.0; // 自动添加球体的时间间隔（秒）

    @property({ type: [Color] })
    ballColors: Color[] = [
        new Color(255, 0, 0, 255),    // 红
        new Color(0, 255, 0, 255),    // 绿
        new Color(0, 0, 255, 255),    // 蓝
        new Color(255, 255, 0, 255),  // 黄
        new Color(255, 0, 255, 255)   // 紫
    ];

    private _orbitController: EllipseOrbitController = null!;
    private _timeSinceLastAdd: number = 0;
    private _generatedBalls: Node[] = []; // 所有生成的球体

    start() {
        // 获取椭圆轨道控制器组件
        if (this.orbitControllerNode) {
            this._orbitController = this.orbitControllerNode.getComponent(EllipseOrbitController)!;
        }

        // 初始化球体
        for (let i = 0; i < this.initialBallCount; i++) {
            this.generateBall();
        }
    }

    update(deltaTime: number) {
        // 自动添加球体逻辑
        this._timeSinceLastAdd += deltaTime;
        if (this._timeSinceLastAdd >= this.autoAddInterval && this._generatedBalls.length < this.maxBallCount) {
            this._timeSinceLastAdd = 0;
            this.generateBall();
        }
    }

    // 生成新球体
    generateBall() {
        if (!this.ballPrefab || !this._orbitController || this._generatedBalls.length >= this.maxBallCount) return;

        // 实例化新球体
        const newBall = instantiate(this.ballPrefab);
        if (!newBall) return;

        // 设置球体颜色
        const sprite = newBall.getComponent(Sprite);
        if (sprite && this.ballColors.length > 0) {
            const colorIndex = this._generatedBalls.length % this.ballColors.length;
            sprite.color = this.ballColors[colorIndex];
        }

        // 初始位置设置为椭圆中心
        newBall.setPosition(new Vec3(0, 0, 0));

        // 添加到场景中
        this.node.addChild(newBall);

        // 添加到轨道控制器
        this._orbitController.addBallToOrbit(newBall);

        // 保存到生成的球体列表
        this._generatedBalls.push(newBall);
    }

    // 移除最后一个球体
    removeLastBall() {
        if (this._generatedBalls.length === 0 || !this._orbitController) return;

        const lastBall = this._generatedBalls.pop();
        if (lastBall && lastBall.isValid) {
            this._orbitController.removeBallFromOrbit(lastBall);
            lastBall.destroy();
        }
    }

    // 移除所有球体
    clearAllBalls() {
        if (!this._orbitController) return;

        // 从轨道控制器移除所有球体
        this._orbitController.clearAllBalls();

        // 销毁所有生成的球体
        for (const ball of this._generatedBalls) {
            if (ball && ball.isValid) {
                ball.destroy();
            }
        }

        this._generatedBalls = [];
    }

    // 重新开始
    restart() {
        this.clearAllBalls();
        for (let i = 0; i < this.initialBallCount; i++) {
            this.generateBall();
        }
    }

    // UI 按钮方法
    onAddBallButtonClick() { this.generateBall(); }
    onRemoveBallButtonClick() { this.removeLastBall(); }
    onClearBallsButtonClick() { this.clearAllBalls(); }
    onRestartButtonClick() { this.restart(); }
}